Skip to content

Conversation

JebediahS
Copy link

Filling the stack should start from element with index 0.

Filling the stack should start from element with index 0.
Copy link
Owner

@ohkimur ohkimur left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! The change from pre-increment/decrement to post-increment/decrement looks correct and fixes the off‑by‑one behavior:

  • In push, stack[stack_pointer++] = element; is the right choice if stack_pointer tracks the next free slot.
  • In pop, return stack[--stack_pointer]; correctly moves back to the last pushed element before reading.

A couple of suggestions to strengthen this and future contributions:

  • Add brief comments documenting the stack invariant: “stack_pointer points to the next free slot.”
  • Include small tests (or a demo main) for: push/pop single item, push until full, pop until empty, and verify strict LIFO.
  • Consider returning a status from push/pop (e.g., int/bool with out‑param) instead of printing errors; returning 0 from pop is ambiguous if 0.0 is a valid value.
  • Use size_t for stack_pointer and comparisons with STACK_SIZE.
  • Optionally guard against overflow/underflow with asserts in debug builds.

Overall, nice, focused fix with clear correctness improvement. If you’re up for it, similar sanity checks and invariants across other stack/queue exercises would be super valuable.

@JebediahS
Copy link
Author

Thanks for the PR! The change from pre-increment/decrement to post-increment/decrement looks correct and fixes the off‑by‑one behavior:

* In `push`, `stack[stack_pointer++] = element;` is the right choice if `stack_pointer` tracks the next free slot.

* In `pop`, `return stack[--stack_pointer];` correctly moves back to the last pushed element before reading.

A couple of suggestions to strengthen this and future contributions:

* Add brief comments documenting the stack invariant: “`stack_pointer` points to the next free slot.”

* Include small tests (or a demo `main`) for: push/pop single item, push until full, pop until empty, and verify strict LIFO.

* Consider returning a status from `push`/`pop` (e.g., int/bool with out‑param) instead of printing errors; returning `0` from `pop` is ambiguous if `0.0` is a valid value.

* Use `size_t` for `stack_pointer` and comparisons with `STACK_SIZE`.

* Optionally guard against overflow/underflow with asserts in debug builds.

Overall, nice, focused fix with clear correctness improvement. If you’re up for it, similar sanity checks and invariants across other stack/queue exercises would be super valuable.

Thank you for the suggestions, I would be happy to contribute more (if my time allows)!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants